/**
 * All rights Reserved
 *
 * @Title: MongoService.java
 * @Package com.amigo.online.provider.sync.abs
 * @Description: (用一句话描述该文件做什么)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午7:11:47
 * @version V1.0
 * @Copyright: 2018  All rights reserved.
 */
package com.amigo.online.provider.sync.abs;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.common.ConstantValue;
import com.mongodb.client.result.UpdateResult;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述） 
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午7:11:47  
 *
 */

@Service
public class MongoService {

    protected static final Logger logger = LoggerFactory.getLogger(MongoService.class);

    @Autowired
    ApplicationContext applicationContext;

    public void insertObject(Document doc, String templateName, String collection) {
        ((MongoTemplate) applicationContext.getBean(templateName)).getCollection(collection).insertOne(doc);
    }

    public long updateObject(Query query, Update update, String templateName, String collection) {
        UpdateResult result = ((MongoTemplate) applicationContext.getBean(templateName)).updateFirst(query, update, collection);
        if (result != null) {
            //logger.info(collection + " mongo 更新结果" + result.toString());
            return result.getModifiedCount();
        } else {

            return 0;
        }
    }

    public long deleteObject(Query query, String templateName, String collection) {
        ((MongoTemplate) applicationContext.getBean(templateName)).remove(query, ConstantValue.USER_MONGO);
        return 0L;
    }
}
